package com.zyc.app.woa.utils;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zyc.app.woa.WxConfig;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author: yingchang.zhang
 * Date  : 2014/11/28
 */
public class StorageUtils {
    public static final String RES_FOLDER = "res";
    public static final String ARTICLE_HTML_FILE = "content.html";
    private static final Pattern FILE_NAME_PATTERN = Pattern.compile("\\w*[.]*\\w+$");


    public static String getAccountArticlesCachePath(String wxId) {
        return WxConfig.ARTICLE_CACHE_PATH + File.separator + wxId;
    }

    public static String getFileName(String url) {
        String fileName = url;
        Matcher fileMatcher = FILE_NAME_PATTERN.matcher(url);
        if (fileMatcher.find()) {
            fileName = fileMatcher.group(0);
        }

        return fileName;
    }

    public static String getAccountArticlePath(String accountArticlesPath, String msgId) {
        return accountArticlesPath + File.separator + msgId;
    }

    public static boolean checkFileExist(String path) {
        File cacheFile = new File(path);
        return cacheFile.exists();
    }

    public static boolean createPathIfNotExist(String path) {
        File cacheFile = new File(path);
        if (!cacheFile.exists()) {
            cacheFile.mkdir();
            return false;
        } else {
            return true;
        }
    }

    public static File createFileIfNotExist(String fullPath) throws IOException {
        String path = fullPath.substring(0, fullPath.lastIndexOf(File.separator));
        File savePath = new File(path);
        if (!savePath.exists()) {
            savePath.mkdirs();
        }

        File file = new File(fullPath);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    public static void save(Object data, String path) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;

        try {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(data);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(fos);
            IOUtils.closeQuietly(oos);
        }
    }

    public static Object read(String path) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        Object obj = null;

        try {
            File file = new File(path);
            fis = new FileInputStream(file);
            ois = new ObjectInputStream(fis);
            obj = ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(fis);
            IOUtils.closeQuietly(ois);
        }
        return obj;
    }

    public static void cachePic(String picUrl) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(false).cacheOnDisk(true).build();

        ImageLoader.getInstance().loadImageSync(picUrl, options);
    }
}
